package com.greet.controller.user;

import com.greet.context.BaseContext;
import com.greet.dto.ShoppingCartDTO;
import com.greet.entity.ShoppingCart;
import com.greet.result.Result;
import com.greet.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public Result addItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车， 商品信息为{}", shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> getList(){
        List<ShoppingCart> shoppingCartList = shoppingCartService.getList(BaseContext.getCurrentId());
        return Result.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    public Result clean(){
        shoppingCartService.clean(BaseContext.getCurrentId());
        return Result.success();
    }

    @PostMapping("/sub")
    public Result sub(ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.sub(shoppingCartDTO);
        return Result.success();
    }
}
